// 公共js配置
// 是否为IE浏览器
export const getBrowserInfo = () => {
  var theUA = window.navigator.userAgent.toLowerCase();
  if ((theUA.match(/msie\s\d+/) && theUA.match(/msie\s\d+/)[0]) || (theUA.match(/trident\s?\d+/) && theUA.match(
      /trident\s?\d+/)[0])) {
    var ieVersion = theUA.match(/msie\s\d+/)[0].match(/\d+/)[0] || theUA.match(/trident\s?\d+/)[0];
    if (ieVersion < 10) {
      var str = "您的浏览器版本过低，为了您的体验，建议您升级!";
      var str2 = "<a href='https://www.google.cn/chrome/' target='_blank' style='color:#000'>谷歌</a>，" +
        "<a href='http://www.firefox.com.cn/' target='_blank' style='color:#000'>火狐</a>，" +
        "其他双核极速模式";
      document.writeln(
        "<div style='font-size:22px;font-weight:bold;line-height:40px;text-align:center;color:#fff;background-color:#0cc; height:100%;border:0;position:fixed;top:0;left:0;width:100%;z-index:1234'>" +
        "<h2 style='padding-top:200px;margin:0'><strong>" + str + "<br/></strong></h2><h2>" +
        str2 + "</h2><h2 style='margin:0'><strong>如果您使用的是双核浏览器，请切换到极速模式访问！<br/></strong></h2></div>");
      document.execCommand("Stop");
      return false;
    }
  }
}

// 函数防抖
export const debounce = (func, wait, immediate) => {
  var timeout
  return function () {
    var context = this
    var args = arguments
    var later = function () {
      timeout = null
      if (!immediate) func.apply(context, args)
    }

    var callNow = immediate && !timeout;
    clearTimeout(timeout)

    timeout = setTimeout(later, wait)
    if (callNow) func.apply(context, args)
  }
}

// url地址参数转对象
export const OperatorParam = () => {
  var url = location.href;
  if (url.indexOf('?') > -1) {
    var arr = [];
    var res = {};
    arr = url.split('?')[1].split('&');
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].indexOf('=') != -1) {
        var str = arr[i].split('=');
        res[str[0]] = str[1];
      } else {
        res[arr[i]] = '';
      }
    }
    return res;
  } else {
    return false;
  }
}

// 匹配url中的字符串参数
export const parseQueryString = (url) => {
  var regUrl = /^[^?]+\?([\w\W]+)$/
  var regPara = /([^&=]+)=([\w\W]*?)(&|$)/g
  var arrUrl = regUrl.exec(url)
  var ret = {}
  if (arrUrl && arrUrl[1]) {
    var strPara = arrUrl[1]
    var result
    while ((result = regPara.exec(strPara)) != null) {
      ret[result[1]] = result[2]
    }
  }
  return ret
}

// 对象转字符
export const objToStr = (param, split) => {
  var str = '';
  var temp = ""
  param = Object.values(param);
  for (var i in param) {
    if (i < param.length - 1) {
      temp = param[i] + split;
      str = str.concat(temp);

    } else {
      temp = param[i];
      str = str.concat(temp);
    }
  }
  return str;
}



// 对象深克隆
export const deepCloneObj = (obj) => {
  // 定义一个变量来存储结果
  var result
  // 判断传递的类型
  if (obj === null || typeof obj !== 'object') return obj;
  // 处理日期对象
  if (obj instanceof Date) {
    result = new Date()
    result.setTime(obj.getTime());
    return result
  }
  // 处理数组
  if (obj instanceof Array) {
    result = [];
    for (var i = 0; i < obj.length; i++) {
      result[i] = deepCloneObj(obj[i])
    }
    return result
  }
  // 处理对象
  if (obj instanceof Object) {
    result = {}
    for (var key in obj) {
      // if (obj.hasOwnProperty(key)) result[key] = deepCloneObj(obj[key])
      if (Object.prototype.hasOwnProperty.call(obj, key)) result[key] = deepCloneObj(obj[key])
    }
    return result
  }
}

// 字符串加密
export const compileStr = (code) => {
  var c = String.fromCharCode(code.charCodeAt(0) + code.length)
  for (var i = 1; i < code.length; i++) {
    c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1))
  }
  return escape(c)
}

// 字符串解密
export const uncompileStr = (code) => {
  code = unescape(code)
  var c = String.fromCharCode(code.charCodeAt(0) - code.length)
  for (var i = 1; i < code.length; i++) {
    c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1))
  }
  return c
}